package com.ruoyi.weixin.shop.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.weixin.shop.domain.vo.WxShopAddBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopEditBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopQueryBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopVo;
import com.ruoyi.weixin.shop.service.IWxShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 微信商城Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Api(value = "微信商城控制器", tags = {"微信商城管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/shop/shop")
public class WxShopController extends BaseController {

	private final IWxShopService iWxShopService;

	/**
	 * 查询微信商城列表
	 */
	@ApiOperation("查询微信商城列表")
	@PreAuthorize("@ss.hasPermi('system:shop:list')")
	@GetMapping("/list")
	public TableDataInfo<WxShopVo> list(@Validated WxShopQueryBo bo) {
		return iWxShopService.queryPageList(bo);
	}

	/**
	 * 导出微信商城列表
	 */
	@ApiOperation("导出微信商城列表")
	@PreAuthorize("@ss.hasPermi('system:shop:export')")
	@GetMapping("/export")
	public AjaxResult<WxShopVo> export(@Validated WxShopQueryBo bo) {
		List<WxShopVo> list = iWxShopService.queryList(bo);
		ExcelUtil<WxShopVo> util = new ExcelUtil<WxShopVo>(WxShopVo.class);
		return util.exportExcel(list, "微信商城");
	}

	/**
	 * 获取微信商城详细信息
	 */
	@ApiOperation("获取微信商城详细信息")
	@PreAuthorize("@ss.hasPermi('system:shop:query')")
	@GetMapping("/{id}")
	public AjaxResult<WxShopVo> getInfo(@NotNull(message = "主键不能为空")
										@PathVariable("id") Long id) {
		return AjaxResult.success(iWxShopService.queryById(id));
	}

	/**
	 * 新增微信商城
	 */
	@ApiOperation("新增微信商城")
	@PreAuthorize("@ss.hasPermi('system:shop:add')")
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody WxShopAddBo bo) {
		return toAjax(iWxShopService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改微信商城
	 */
	@ApiOperation("修改微信商城")
	@PreAuthorize("@ss.hasPermi('system:shop:edit')")
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody WxShopEditBo bo) {
		return toAjax(iWxShopService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除微信商城
	 */
	@ApiOperation("删除微信商城")
	@PreAuthorize("@ss.hasPermi('system:shop:remove')")
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iWxShopService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}

